<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Database\Seeder;
use Faker;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker\Factory::create();
        User::factory(5)->create();
        User::create([
            'first_name'=>'Bill',
            'last_name'=>'Clinton',
            'email'=>'bill@gmail.com',
            'password'=>password_hash('bill',PASSWORD_DEFAULT),
            'role'=>'admin'
        ]);
        User::create([
            'first_name'=>'John',
            'last_name'=>'Doe',
            'email'=>'john@gmail.com',
            'password'=>password_hash('john',PASSWORD_DEFAULT),
            'role'=>'admin'
        ]);
        Post::factory(50)->create();
        Comment::factory(50)->create();
        Comment::factory(50)->create();
        Comment::factory(50)->create();
        Comment::factory(50)->create();

        $tags = ['Story','Recipe','Economy',
            'Literature','Art','Sport',
            'News','Movie','Book','Weather',
            'Climate','Industry'
            ];
        foreach($tags as $tag){
            Tag::create(['name'=>$tag]);
        }
        $tag_ids = Tag::pluck('id');
        $posts = Post::all();
        foreach($posts as $post){
            $num = $faker->numberBetween(1,5);
            $faker->unique(true);
            for($i=0;$i<$num;$i++){
                $tag_id = $faker->unique()->randomElement($tag_ids);
                $post->tags()->attach($tag_id);
            }
        }
    }
}
